<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Services_Web
{
	public static function validate($webData)
	{
		$validator = new Gio_Core_Validator();
		foreach ($webData as $index => $value) {
			if (gettype($value) == 'string') {
				$webData[$index] = trim($value);
			}
		} 
		$webData['messages']['web_id'] = $validator->setString($webData['web_id'])
												->validate(array('notNull', 'isIdString')
																, array(
																	'notNull' 		=> 'web_id_notnull',
																	'isIdString'	=> 'web_id_invalid'
																)
														);
		$webData['messages']['name'] = $validator->setString($webData['name'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'web_name_notnull'
																)
														);
		$webData['messages']['screen_name'] = $validator->setString($webData['screen_name'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'web_screen_name_notnull'
																)
														);
														
		$error = false;
		foreach ($webData['messages'] as $index => $message) {
			if ($message) {
				$webData['messages_error'] = true;
				return $webData;
			}
		}										
		$webData['messages_error'] = false;
		return $webData; 
	}
	
	public static function getById($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->getById($id);
	}
	
	public static function getAllWebs()
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->getAllWebs();
	}
	
	public static function add($web)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->add($web);
	}
	
	public static function delete($webId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->delete($webId);
	}
	
	public static function update($web)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->update($web);
	}
	
	public static function find($condition = array(), $offset = null, $limit = null)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->find($condition, $offset, $limit);
	}
	
	public static function count($condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$webDao = new Modules_News_Models_Mysql_Web();
		$webDao->setConnection($conn);
		return $webDao->count($condition);
	}
}